﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;

namespace DanielsGameLibrary
{
    public class Camera
    {

        public Vector3 position { get; set; }
        public Vector3 angle;

        public Matrix ViewMatrix { get; set; }
        public Matrix ProjectionMatrix { get; set; }

        public Movable attachment { get; set; }

        public bool lookatt;
        public float offset;

        public Camera(Game game, float ViewAngle, float NearClip, float FarClip)
        {
            position = new Vector3(0, 10, 0);
            ViewMatrix = Matrix.Identity;
            ProjectionMatrix = Matrix.Identity;
            angle = Vector3.Zero;

            try
            {
                float ratio = (float)game.GraphicsDevice.Viewport.Width / (float)game.GraphicsDevice.Viewport.Height;
                ProjectionMatrix = Matrix.CreatePerspectiveFieldOfView( ViewAngle, ratio, NearClip, FarClip);
            }
            catch
            { }
        }

        public Vector3 CameraDir()
        {
            Matrix m = Matrix.Identity;
            m *= Matrix.CreateRotationZ(attachment.Orientation.Z);
            m *= Matrix.CreateRotationY(attachment.Orientation.Y);
            m *= Matrix.CreateRotationX(attachment.Orientation.X);

            return Vector3.Transform(Vector3.Forward, m);
        }

        public void Update()
        {
            try
            {
                if (!lookatt)
                {

                    ViewMatrix = Matrix.Identity;
                    ViewMatrix *= Matrix.CreateTranslation(-attachment.Position);
                    ViewMatrix *= Matrix.CreateRotationZ(attachment.Orientation.Z);
                    ViewMatrix *= Matrix.CreateRotationY(attachment.Orientation.Y);
                    ViewMatrix *= Matrix.CreateRotationX(attachment.Orientation.X);
                    position = attachment.Position;
                    angle = attachment.Orientation;
                }
                else
                {
                    Matrix rotation = Matrix.Identity;
                    //rotation *= Matrix.CreateRotationZ(attachment.Orientation.Z);

                    rotation *= Matrix.CreateRotationX(-attachment.Orientation.X);
                    rotation *= Matrix.CreateRotationY(-attachment.Orientation.Y);
                    position = attachment.Position + Vector3.Transform(offset * Vector3.UnitZ, rotation);

                    ViewMatrix = Matrix.CreateLookAt(position, attachment.Position, Vector3.UnitY);
                }
            }
            catch
            {

                ViewMatrix = Matrix.Identity;
                ViewMatrix *= Matrix.CreateTranslation(-position);
                ViewMatrix *= Matrix.CreateRotationZ(angle.Z);
                ViewMatrix *= Matrix.CreateRotationY(angle.Y);
                ViewMatrix *= Matrix.CreateRotationX(angle.X);
            }
        }
    }
}
